from machine import Pin
import time


class Buzzer:
    def __init__(self, pin) -> None:
        self.buzzer = Pin(pin, Pin.OUT)  # 蜂鸣器初始化
        self.buzzer_playing = 0  # 蜂鸣器是否播放标志位
        self.buzzer_select = {
            "bleopen": self.Bluetooth_Connection,
            "bleclose": self.Bluetooth_Disconnected,
            "buzzer1": self.Buzzer_1,
            "buzzer2": self.Buzzer_2,
            "buzzer3": self.Buzzer_3,
            "task1": self.Buzzer_task1,
            "task2": self.Buzzer_task2,
            "task3": self.Buzzer_task3,
        }  # 蜂鸣器播放的音效字典

# ============================================================================== #

    '''
    蜂鸣器播放的线程，select选择播放的音效
    '''

    def Buzzer_Play(self, select):
        if self.buzzer_playing == 0:
            self.buzzer_playing = 1
            self.buzzer_select.get(select)()
            self.buzzer_playing = 0
        return

# ============================================================================== #

    '''
    蓝牙连接的蜂鸣器音效
    '''

    def Bluetooth_Connection(self):  # 蓝牙连接提示音
        self.buzzer.value(1)
        time.sleep_ms(100)
        self.buzzer.value(0)
        time.sleep_ms(100)
        self.buzzer.value(1)
        time.sleep_ms(200)
        self.buzzer.value(0)
        print("Buzzer_blueopen")

# ============================================================================== #

    '''
    蓝牙断开连接的蜂鸣器音效
    '''

    def Bluetooth_Disconnected(self):  # 蓝牙断开提示音
        self.buzzer.value(1)
        time.sleep_ms(200)
        self.buzzer.value(0)
        time.sleep_ms(100)
        self.buzzer.value(1)
        time.sleep_ms(100)
        self.buzzer.value(0)
        print("Buzzer_bluebreake")

# ============================================================================== #

    '''
    蜂鸣器音效 1
    '''

    def Buzzer_1(self):  # 开机提示音 1
        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)
        print("Buzzer_1")

# ============================================================================== #

    '''
    蜂鸣器音效 2
    '''

    def Buzzer_2(self):  # 提示音 2
        self.buzzer.value(1)
        time.sleep_ms(100)
        self.buzzer.value(0)

        time.sleep_ms(100)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)
        print("Buzzer_2")

# ============================================================================== #

    '''
    蜂鸣器音效3
    '''

    def Buzzer_3(self):  # 提示音 3
        self.buzzer.value(1)
        time.sleep_ms(100)
        self.buzzer.value(0)

        time.sleep_ms(300)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)

        time.sleep_ms(300)

        self.buzzer.value(1)
        time.sleep_ms(100)
        self.buzzer.value(0)

        time.sleep_ms(300)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)
        print("Buzzer_3")

# ============================================================================== #

    '''
    蜂鸣器任务音效 1
    '''

    def Buzzer_task1(self):  # 提示音 3
        self.buzzer.value(1)
        time.sleep_ms(100)
        self.buzzer.value(0)

        time.sleep_ms(300)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)

        time.sleep_ms(300)

        self.buzzer.value(1)
        time.sleep_ms(100)
        self.buzzer.value(0)

        time.sleep_ms(300)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)
        print("Buzzer_task1")

# ============================================================================== #

    '''
    蜂鸣器任务音效 2
    '''

    def Buzzer_task2(self):  # 提示音 3
        self.buzzer.value(1)
        time.sleep_ms(200)
        self.buzzer.value(0)

        time.sleep_ms(200)

        self.buzzer.value(1)
        time.sleep_ms(200)
        self.buzzer.value(0)

        time.sleep_ms(200)

        self.buzzer.value(1)
        time.sleep_ms(200)
        self.buzzer.value(0)

        time.sleep_ms(200)

        self.buzzer.value(1)
        time.sleep_ms(200)
        self.buzzer.value(0)
        print("Buzzer_task2")

# ============================================================================== #

    '''
    蜂鸣器任务音效 3
    '''

    def Buzzer_task3(self):  # 提示音 3
        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)

        time.sleep_ms(100)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)

        time.sleep_ms(100)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)

        time.sleep_ms(100)

        self.buzzer.value(1)
        time.sleep_ms(300)
        self.buzzer.value(0)
        print("Buzzer_task3")
